package com.jincp.filepicker.ui.video;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;

import com.jincp.common.brvah.BaseQuickAdapter;
import com.jincp.common.dialog.DialogDismissListener;
import com.jincp.common.dialog.DialogResultListener;
import com.jincp.common.model.ToolBarOptions;
import com.jincp.common.utils.ToastUtil;
import com.jincp.filepicker.Constant;
import com.jincp.filepicker.R;
import com.jincp.filepicker.adapter.VideoPickAdapter;
import com.jincp.filepicker.base.FileBaseActivity;
import com.jincp.filepicker.bean.Directory;
import com.jincp.filepicker.bean.VideoFile;
import com.jincp.filepicker.loader.FileLoaderCallbacks;
import com.jincp.filepicker.loader.SimpleDataLoader;
import com.jincp.filepicker.widgets.PickDirectoryDialog;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * =========================================================
 *
 * @ProjectName:
 * @ClassName: VideoPickActivity
 * @Description:
 * @Author: jincp
 * @CreateDate: 2019/3/22 21:10
 * =========================================================
 */
public class VideoPickActivity extends FileBaseActivity implements Observer {

    private VideoPickAdapter adapter;
    private Button btnCompleted;
    private Button btnDirectory;

    @Override
    protected int getContentLayout() {
        return R.layout.activity_video_pick;
    }

    @Override
    protected void initViews() {
        setCustomToolBar((Toolbar) findViewById(R.id.toolbar), new ToolBarOptions());
        configRecyclerView();
        btnDirectory = findViewById(R.id.btn_directory);
        btnDirectory.setText("全部视频");
        btnDirectory.setOnClickListener(this);

        btnCompleted = findViewById(R.id.btn_complete);
        btnCompleted.setVisibility(VideoPickerManager.getInstance().isMultiMode() ? View.VISIBLE : View.GONE);
        btnCompleted.setText(R.string.complete);
        btnCompleted.setOnClickListener(this);
    }

    private void configRecyclerView() {
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        GridLayoutManager layoutManager = new GridLayoutManager(this, 3);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new VideoPickAdapter();
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                VideoFile videoFile = (VideoFile) adapter.getItem(position);
                if (videoFile == null) {
                    VideoPickerManager.getInstance().takeVideo(VideoPickActivity.this);
                } else {
                    if (VideoPickerManager.getInstance().isMultiMode()) {
                        VideoPickerManager.getInstance().previewVideo(VideoPickActivity.this, videoFile);
                    } else {
                        VideoPickerManager.getInstance().addSelectedFile(videoFile);
                        VideoPickerManager.getInstance().completedSelector();
                        finish();
                    }
                }

            }
        });
        adapter.bindToRecyclerView(recyclerView);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        VideoPickerManager.getInstance().addObserver(this);
        SimpleDataLoader.getVideos(this, new FileLoaderCallbacks.FileLoadResultCallback<VideoFile>() {
            @Override
            public void onResult(List<Directory<VideoFile>> directoryList) {
                if (directoryList == null || directoryList.isEmpty()){
                    adapter.setEmptyView(R.layout.empty_view);
                    return;
                }
                if (VideoPickerManager.getInstance().isShowCamera()){
                    for (Directory<VideoFile> videoFileDirectory : directoryList) {
                        videoFileDirectory.getFiles().add(0, null);
                    }
                }
                VideoPickerManager.getInstance().setDirectoryList(directoryList);
                Directory firstDirectory = directoryList.get(0);
                firstDirectory.setSelected(true);
                adapter.setNewData(firstDirectory.getFiles());
            }
        });
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view.getId() == R.id.btn_complete) {
            VideoPickerManager.getInstance().completedSelector();
        } else if (view.getId() == R.id.btn_directory) {
            if (VideoPickerManager.getInstance().getDirectoryList() == null){
                ToastUtil.getInstance().show(R.string.empty_no_data);
                return;
            }
            PickDirectoryDialog dialog = new PickDirectoryDialog.Builder()
                    .setSize(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                    .setGravity(Gravity.BOTTOM)
                    .setDirectoryList(VideoPickerManager.getInstance().getDirectoryList())
                    .setDirectoryType(Constant.TYPE_VIDEO)
                    .build();
            dialog.setDialogResultListener(new DialogResultListener() {
                @Override
                public void result(DialogFragment dialog, Object result) {
                    dialog.dismissAllowingStateLoss();
                    int position = (int) result;
                    VideoPickerManager.getInstance().setCurrentDirectoryIndex(position);
                    adapter.setNewData(VideoPickerManager.getInstance().getCurrentDirectoryFileList());
                }
            });
            dialog.setDialogDismissListener(new DialogDismissListener() {
                @Override
                public void dismiss(DialogFragment dialog) {
                    btnDirectory.setEnabled(true);
                }
            });
            dialog.show(getSupportFragmentManager(), "PickDirectoryDialog");
            btnDirectory.setEnabled(false);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        VideoPickerManager.getInstance().onActivityResult(this, requestCode, resultCode, data);
    }

    @Override
    public void update(Observable observable, Object arg) {
        if (observable instanceof VideoPickerManager){
            if (VideoPickerManager.getInstance().getSelectFileCount() > 0) {
                btnCompleted.setText(getString(R.string.selected_complete, VideoPickerManager.getInstance().getSelectFileCount(), VideoPickerManager.getInstance().getMaxSelectedLimit()));
                btnCompleted.setEnabled(true);
            } else {
                btnCompleted.setText(getString(R.string.complete));
                btnCompleted.setEnabled(false);
            }
        }
    }
}